<?
include('../scaffold/content.php');
include('../scaffold/tag.php');
include('../controller/handler.php');
include('../controller/philomousia.php');

include('../module/var.inc.php');

session_start();

$hd = new Handler();
$ph = new Philomousia();
$hd->openDB();
$base_url = $hd->getBaseURL();
$update = false;
if(isset($_GET['id'])) {
	$content = $ph->loadContent($_GET['id'], $hd);
	$update = true;
}
$tags = $ph->loadTags($hd);

//echo print_r($_POST);

if(isset($_POST['delete'])) {
	$deleted = true;
	$ph->deleteContent($_POST['delete'], $hd);
	$feedback = "Articolo eliminato con successo.";
} else if(isset($_POST['modify']) && $hd->getPassword()==sha1($_SESSION['password'])) {
	$updated = true;
	$ph->updateContent($_POST['id'], $_POST['title'], $_POST['author'], $_POST['body'], $_POST['tag'], $_POST['type'], $hd);
	$feedback = "Articolo <a href=\"".$base_url.$links[$_POST['type']]."/post:".$_POST['id']."\"><span class=\"italic\">".stripslashes($_POST['title'])."</span></a> modificato con successo.";
}
$hd->closeDB();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">
<head>
	<? include('../module/head.inc.php'); ?>
	<meta name="robots" content="noindex, nofollow"/>
	<script src="<?echo $base_url;?>js/editor.js" type="text/javascript" charset="utf-8"></script>
</head>

<body>
<div id="content">
<?include('../module/header.inc.php');?>

<?if($hd->getPassword()==sha1($_SESSION['password'])) {?>

<div class="form">
	<div class="upper_bar_main"><div class="upper_bar_round"></div></div>
	<div class="body_main_left">
	<div class="body_main_right">
	<div class="body_main">
	<form action="manage" method="post" accept-charset="utf-8" onsubmit="return articleForm(this);;">	
		<h2>Gestione articolo</h2>
		<?if($update) {?>
		<table border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td width="380px">
					<input type="text" class="text" name="title" value=" <?echo $content->getTitle();?>" /> Titolo<br />
					<input type="text" class="text" name="author" value="<?echo $content->getAuthor();?>" /> Autore<br />
				</td><td></td></tr>
			<tr>
				<td><textarea class="content" id="txt" name="body" rows="24" cols="45"><?echo $hd->reverseString($content->getBody()); ?></textarea></td>
				<td>
					<?include("../module/editor.inc.php");?>
					<div id="tag_box">
					<?
					$total = count($tags);
					?>
					<input type="hidden" name="no_tags" value="false" />
						<table border="0" cellspacing="2" cellpadding="0">
							<tr>
							<?
							$i = 0;
							foreach($tags as $tag) {
								$i++;
							?><td><input type="checkbox" name="tag[<?echo $tag->getId();?>]" value="<?echo $tag->getId();?>"
								<?foreach($content->getTags() as $c_tag) { if($c_tag->getId()==$tag->getId()) echo "checked";}?> /> 
							<?echo $tag->getName();?></td>
							<?
								if($i%2==0 && $total>$i) echo "</tr><tr>";
							}
							?>
							</tr>
						</table>
					</div>
					<select name="type" >
						<option value="0" <?if($content->getType()==0) { echo "selected";}?>><?echo $section[0];?></option>
						<option value="1" <?if($content->getType()==1) { echo "selected";}?>><?echo $section[1];?></option>
						<option value="2" <?if($content->getType()==2) { echo "selected";}?>><?echo $section[2];?></option>
					</select>
					<input type="hidden" name="id" value="<?echo $_GET['id'];?>" />
					<input type="hidden" name="modify" value="true" />
				</td>
			</tr>
		</table>
		<p><input type="button" value="Anteprima &rarr;" onclick="showPreview('txt', '<?echo $base_url?>')" />
			<input type="submit" value="Modifica &rarr;" />
			<input type="button" value="Elimina &rarr;" onclick="deleteArticle('delete_form')" /></p>
	</form>
	<form action="manage" method="post" accept-charset="utf-8" id="delete_form">
		<input type="hidden" name="delete" value="<?echo $_GET['id'];?>" />
	</form>
	<?}?>
	<?if($feedback) {?><p id="feedback" class="highline"><?echo $feedback;?></p> <?}?>
	<p class="small admin_action">
		<a href="<?echo $base_url;?>panel">&larr; Pannello di amministrazione</a>
		<?if($update) {?>
		 | <a href="<?echo $base_url.$links[$content->getType()];?>/post:<?echo $content->getId()?>">AnNULLa modifiche</a>
		<?} else if($updated) {?>
		 | <a href="<?echo $base_url.$links[$_POST['type']];?>/post:<?echo $_POST['id']?>">Guarda articolo</a>
		<?}?>
	</p>
	</div></div></div>
	<div class="lower_bar_main"><div class="lower_bar_round"></div></div>
</div>

<?include('../module/preview.inc.php');?>
<?include('../module/uploaded.img.inc.php');?>

<?}?>

<? include('../module/footer.inc.php'); ?>
</div>
</body>
</html>
